package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
@RequestMapping("/depts")
public class DeptController {

    //注入Service接口
    @Autowired
    private DeptService deptService;

    /**
     * 部门列表的查询
     *
     * @return
     */
    @GetMapping
    public Result list() {
        //调用service查询数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 根据id删除部门
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        //调用service删除部门
        deptService.deleteById(id);
        //响应成功
        return Result.success();
    }

    /**
     * 添加部门
     *
     * @param dept
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Dept dept) {
        //调用service保存部门
        deptService.save(dept);
        //响应成功
        return Result.success();
    }
}
